﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;

namespace Com
{
    /// <summary>
    /// Interaction logic for ucColdStorageProduct.xaml
    /// </summary>
    public partial class ucColdStorageProduct : UserControl
    {
        int productType = 1;
        bool cbxChange = false;
        tb_product selectedProduct;
        tb_respiratory_heat selectedRepiatory;
        public ucColdStorageProduct()
        {
            this.InitializeComponent();
            loadData();
            enableButtons();

        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnNew_Copy_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {

        }

        private void enableButtons()
        {
            btnCancel.IsEnabled = false;
            btnSave.IsEnabled = false;

            btnNew.IsEnabled = true;
            btnCopy.IsEnabled = true;
            btnDelete.IsEnabled = true;
        }
        private void disableButtons()
        {
            btnCancel.IsEnabled = true;
            btnSave.IsEnabled = true;

            btnNew.IsEnabled = false;
            btnCopy.IsEnabled = false;
            btnDelete.IsEnabled = false;
        }
        private void loadData()
        {
            LibraryManagerInCom.loadProduct();
            cbxDescription.Items.Clear();
            foreach (var item in LibraryManagerInCom.ProductList)
            {
                if (item.product_type == productType)
                {
                    cbxDescription.Items.Add(item);
                }

            }
            cbxDescription.DisplayMemberPath = "product_name";
            cbxDescription.SelectedIndex = 0;
        }

        private void rdoMeat_Checked(object sender, RoutedEventArgs e)
        {
            productType = 1;
            loadData();
            cbxTemperature.IsEnabled = false;
            txtHeat.IsEnabled = false;
        }

        private void rdoVegetable_Checked(object sender, RoutedEventArgs e)
        {
            productType = 2;
            loadData();
            cbxTemperature.IsEnabled = true;
            txtHeat.IsEnabled = true;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                selectedProduct = (tb_product)cbxDescription.SelectedItem;
                txtSpecificHeat.Text = selectedProduct.specific_heat.ToString();
                
                //  Kiểm tra nếu là thực vật thì hiển thị nhiệt do hô hấp
                if (productType == 2)
                {
                    // Load nhiệt độ lên trên combobox Temperature
                    loadRespiratoryTemperature(selectedProduct);
                }

                // Load hệ số enthanlpy
                LibraryManagerInCom.loadEnthapyList();
                dtgEnthlpy.ItemsSource = LibraryManagerInCom.eco.tb_enthalpy;
                //foreach(var item in LibraryManagerInCom.loadEnthapyList)
                //{
                //    dtgEnthlpy.DataContext = item;
                //}

            }
            catch (Exception)
            {

                //MessageBox.Show(ex.Message);
            }

            //}

        }

        private void loadRespiratoryTemperature(tb_product pr)
        {
            try
            {
                cbxTemperature.Items.Clear();
                LibraryManagerInCom.loadRespirationList();
                foreach (var item in LibraryManagerInCom.RespirationList)
                {
                    if (item.product_id == pr.id)
                    {
                        cbxTemperature.Items.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
            LibraryManagerInCom.loadRespirationList();
            //tb_respiratory_heat aa = LibraryManagerInCom.RespirationList.
          


            cbxTemperature.DisplayMemberPath = "temperature";
            cbxTemperature.SelectedIndex = 0;
        }

        private void cbxTemperature_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in LibraryManagerInCom.RespirationList)
            {
                if (item.temperature == ((tb_respiratory_heat)cbxTemperature.SelectedItem).temperature)
                {
                    txtHeat.Text = item.heat.ToString();
                }
            }
        }

    }
}